/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package object;

import java.util.List;
import static object.Piece.SQUARE_BORDER;
import ui.GamePanel;

/**
 *
 * @author XiaoLong
 */
public class Control {

    private GamePanel gamePanel;
    private Piece piece;
    private int[][] squares;
    private List<Square> squaresList;

    public Control(GamePanel gamePanel, Piece piece, int[][] squares) {
        this.gamePanel = gamePanel;
        this.piece = piece;
        this.squares = squares;
    }
    //按键盘上时触发的方法

    public boolean tryChange() {
        squaresList = this.piece.getNextChange();
        for (int i = 0; i < squaresList.size(); i++) {
            Square s;
            s = squaresList.get(i);
            if (s.getBeginY() + SQUARE_BORDER > this.gamePanel.getHeight()) {//如果超过底部
                return false;
            }
            //查找界面数组中的Square对象
            int block = getSquare(s.getBeginX(), s.getBeginY());
            //block非空表示重叠
            if (block != 0) {
                return false;
            }
        }
        return true;
    }

    public void change() {
        if (this.piece == null) {
            return;
        }
        if (tryChange()) {
            this.piece.change();
            //this.gamePanel.repaint();
        }
        //判断转换后左边是否越界
        //得到当前方块最小的X座标
        int minX = this.piece.getMinXLocation();
        //左边越界
        if (minX < 0) {
            //右移超过的部分
            this.piece.setSquaresXLocation(-minX);
        }
        //判断转换后右边是否越界
        int maxX = this.piece.getMaxXLocation();
        int gamePanelWidth = this.gamePanel.getWidth();
        //右边越界
        if (maxX > gamePanelWidth) {
            //左移超过GamePanel宽的部分
            this.piece.setSquaresXLocation(-(maxX - gamePanelWidth));
        }
        this.gamePanel.repaint();
    }

    //右, 参数为距离(一格)
    public void right(int size) {

        if (this.piece == null) {
            return;
        }
        //判断右边是否有Square
        if (isRightBolck()) {
            return;
        }
        //判断是否超过GamePanel的宽
        if (this.piece.getMaxXLocation() >= this.gamePanel.getWidth()) {
            return;
        }
        int distance = Piece.SQUARE_BORDER * size;
        this.piece.setSquaresXLocation(distance);
        this.gamePanel.repaint();
    }

    //左, 参数为距离(一格)
    public void left(int size) {
        if (this.piece == null) {
            return;
        }
        //判断左边是否有Square
        if (isLeftBlock()) {
            return;
        }
        //判断是否已经在最左边边界
        if (this.piece.getMinXLocation() <= 0) {
            return;
        }
        //得出移动距离
        int distance = -(Piece.SQUARE_BORDER * size);
        this.piece.setSquaresXLocation(distance);
        this.gamePanel.repaint();
    }

    //判断当前的Piece对象左边是否有障碍, 返回true表示有, 返回false表示没有
    private boolean isLeftBlock() {
        squaresList = this.piece.getSquares();
        for (int i = 0; i < squaresList.size(); i++) {
            Square s = squaresList.get(i);
            //查找界面数组中的Square对象
            int block = getSquare(s.getBeginX() - Piece.SQUARE_BORDER, s.getBeginY());
            //block非空表示遇到障碍
            if (block != 0) {
                return true;
            }
        }
        return false;
    }

    //判断当前的Piece对象右边是否有障碍, 返回true表示有, 返回false表示没有
    private boolean isRightBolck() {
        squaresList = this.piece.getSquares();
        for (int i = 0; i < squaresList.size(); i++) {
            Square s = squaresList.get(i);
            //查找界面数组中的Square对象
            int block = getSquare(s.getBeginX() + Piece.SQUARE_BORDER, s.getBeginY());
            //block非空表示遇到障碍
            if (block != 0) {
                return true;
            }
        }
        return false;
    }

    //下
    public void down() {
        if (this.piece == null) {
            return;
        }
        if (isBlock() || isButtom()) {
            return;
        }
        //判断快整下降后是否有障碍或者到底部
        this.piece.setSquaresYLocation(Piece.SQUARE_BORDER);
        //改变位置后再判断是否需要显示下一个
        if (isBlock() || isButtom()) {
            this.gamePanel.showNext();
        }
        this.gamePanel.repaint();
    }

    //加速下
    public void quickDown() {
        //判断快整下降后是否有障碍或者到底部
        while (!isBlock() && !isButtom()) {
            this.piece.setSquaresYLocation(Piece.SQUARE_BORDER);
        }
        this.gamePanel.repaint();
    }

//根据开始座标在当前界面数组中查找相应的Square
    private int getSquare(int beginX, int beginY) {
        int i = (beginX / Piece.SQUARE_BORDER);
        int j = (beginY / Piece.SQUARE_BORDER);
        if (i < 0 || j < 0) {
            return 0;
        }
        if (i > GamePanel.WIDTHSIZE - 1 || j > GamePanel.HIGHTSIZE - 1) {
            return 0;
        }
        int s = this.squares[i][j];
        //与参数的开始座标相同并且图片不为空
        if (s != 0) {
            return s;
        }
        return 0;
    }

    //判断是否到最底部
    public boolean isButtom() {
        if (this.piece.getMaxYLocation() >= this.gamePanel.getHeight()) {
            return true;
        }
        return false;
    }

    //判断当前的Piece是否遇到障碍, 返回true表示遇到障碍, 返回false表示没有遇到
    public boolean isBlock() {

        squaresList = this.piece.getSquares();
        for (int i = 0; i < squaresList.size(); i++) {
            Square s;
            s = squaresList.get(i);
            //查找界面数组中的Square对象, 注意要将Y加上边距, 因为Y是开始座标
            //需要拿一个Square的最大Y座标
            int block = getSquare(s.getBeginX(), s.getBeginY() + Piece.SQUARE_BORDER);
            //block非空表示遇到障碍
            if (block != 0) {
                return true;
            }
        }
        return false;
    }

    public Piece getPiece() {
        return piece;
    }

    public void setPiece(Piece piece) {
        this.piece = piece;
    }

    public int[][] getSquares() {
        return squares;
    }

    public void setSquares(int[][] squares) {
        this.squares = squares;
    }
}
